{include file="common/header" /}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-sm layuiadmin-btn-admin" href="{:url('article/add')}">添加</a> <!--data-type="add"-->
                </div>
                <div class="layui-inline">
                    <input type="text" name="title" placeholder="标题" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                        搜索
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-user-table" lay-filter="LAY-user-table">
                
            </table>
        </div>
    </div>
</div>

<script type="text/html" id="statusTpl">
    {{#  if(d.is_publish == 1){ }}
    <button class="layui-btn layui-btn-success layui-btn-xs">已发布</button>
    {{#  } else { }}
    <button class="layui-btn layui-btn-danger layui-btn-xs">未发布</button>
    {{#  } }}
</script>
<script type="text/html" id="table-seller-admin">
    {if (buttonAuth('article/edit'))}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    {/if}
    {if (buttonAuth('article/del'))}
    {{#  if(d.admin_id == '1'){ }}
    <a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-delete"></i>删除</a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
    {{#  } }}
    {/if}
</script>

{include file="common/footer" /}
<script>
    layui.config({
        base: '/static/admin/'
    }).use(['table'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;

        var active = {

            add: function() {
                layTool.open( "{:url('manager/addAdmin')}", "添加管理员", '50%', '50%');
            }
        };

        $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 监听搜索
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;

            // 执行重载
            table.reload('LAY-user-table', {
                where: field
            });
        });
    });

    renderTable();
    // 渲染表格
    function renderTable() {
        layTool.table("#LAY-user-table", "/admin/article/index", [
            [
                {
                    field: "id",
                    title: "ID"
                }, 
                {
                    field: "title",
                    title: "标题",
                }, 
                {
                    field: "category_zh",
                    title: "分类",
                },
                {
                    field: "author",
                    title: "作者",
                },
                 {
                    field: "is_publish_zh",
                    title: "发布状态",
                    templet: '#statusTpl'
                }, 
                {
                    field: "create_date",
                    title: "发布时间",
                }, 
                {
                    title: "操作",
                    align: "center",
                    width: 150,
                    fixed: "right",
                    toolbar: "#table-seller-admin"
                }
            ]
        ]);

        layui.use(['table', 'layer'], function () {
            let layer = layui.layer;
            let table = layui.table;

            table.on("tool(LAY-user-table)",
                function(e) {
                    if ("del" === e.event) {
                        layer.ready(function () {
                            var index = layer.confirm('您确定要删除该条数据？', {
                                title: '友情提示',
                                icon: 3,
                                btn: ['确定', '取消']
                            }, function() {
                                $.getJSON('{:url("article/del")}', {id: e.data.id}, function (res) {
                                    if(0 == res.code) {
                                        layer.msg(res.msg);
                                        setTimeout(function () {
                                            renderTable();
                                        }, 300);
                                    } else {
                                        layer.alert(res.msg);
                                    }
                                });
                            }, function(){

                            });
                        });
                    } else if ("edit" === e.event) {
                        window.open("/admin/article/edit/id/" + e.data.id, '_self');
                    }
                });
        });
    }
</script>

